#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    const static int N = 1e5;
    vector<vector<int>> pic;
    bool vis[N] = { false };

    int res = 0;
    int dfs(int cur)
    {
        vis[cur] = true;
        int tmp = pic[cur].size();
        if (tmp == 0)
        {
            ++res;
            return 1;
        }
        int sum = 0;
        int prev = 0;
        bool flag = true;
        for (auto num : pic[cur])
        {
            if (!vis[num])
            {
                int rec = dfs(num);
                if (prev == 0) prev = rec;
                else if (prev != rec) flag = false;
                sum += rec;
            }
        }
        if (flag) ++res;
        return sum + 1;
    }

    int countGoodNodes(vector<vector<int>>& edges) {
        int n = edges.size() + 1;
        pic = vector<vector<int>>(n);
        for (auto vec : edges)
        {
            pic[vec[0]].push_back(vec[1]);
            pic[vec[1]].push_back(vec[0]);
        }
        dfs(0);
        return res;
    }
};